package com.example.common.commonsutils;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author:tjf
 * @Description:
 */
public class DateFormatUtils {
    private static final String ULOG_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ssX";
    private static ThreadLocal<DateFormat> threadLocalDateFormat = new ThreadLocal<>();

    public static DateFormat getUlogDateFormat() {
        DateFormat dateFormat = threadLocalDateFormat.get();
        if (dateFormat == null) {
            dateFormat = new SimpleDateFormat(ULOG_DATE_PATTERN);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            threadLocalDateFormat.set(dateFormat);
        }
        return dateFormat;
    }

    public static Date getUlogTimestamp(String ulogDateStr) throws ParseException {
        return getUlogDateFormat().parse(ulogDateStr);
    }
    public static String getDateStr(){
        Date date = DateUtils.addDays(new Date(), -1);
        String dateStr = getUlogDateFormat().format(date).split("T")[0];
        return dateStr;
    }
}
